//
//  ActivateFirstCell.m
//  JiuLe
//
//  Created by xcq on 15/11/6.
//  Copyright © 2015年 Fine. All rights reserved.
//
#import "Common.h"
#import "ActivateFirstCell.h"
@interface ActivateFirstCell ()<UITextFieldDelegate>
@end

@implementation ActivateFirstCell

- (void)awakeFromNib {
    
    if ([self respondsToSelector:@selector(setSeparatorInset:)]) {
        [self setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    if ([self respondsToSelector:@selector(setLayoutMargins:)]) {
        [self setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(textFiledEditChanged:)
                                                name:UITextFieldTextDidChangeNotification
                                              object:_nickName];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *toBeString = [textField.text
                            stringByReplacingCharactersInRange:range
                            withString:string];
    if (textField.tag != _nickName.tag) {
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:kNumbers] invertedSet];
        NSString *filtered =
        [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        BOOL bol = [string isEqualToString:filtered];
        if (textField.tag == _phoneNum.tag) {
            if (toBeString.length <= 11 && bol) {
                return YES;
            }
            return NO;
        }
    } else {
        BOOL bol1 = [Common verificationNickName:string];
        if (!bol1 || toBeString.length > 15) {
            return NO;
        }
    }
    return YES;
}

- (void)textFiledEditChanged:(NSNotification *)obj {
    UITextField *textField = (UITextField *)obj.object;
    if (textField != _nickName) {
        return ;
    }
    NSString *toBeString = textField.text;
     NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textField markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        if (position) {
            // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
            if (toBeString.length > 14) {
                _nickName.text = [toBeString substringToIndex:15];
            }
        }
    }
}


- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField.tag == _nickName.tag) {
        _endEditAction(textField.text,1);
    } else {
        _endEditAction(textField.text,2);
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
